<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="smvc" value="${pageContext.request.contextPath}" />

<script>
  wx.config({
      debug: false,
      appId: '${appid}',
      timestamp: '${sign.timestamp}',
      nonceStr: '${sign.noncestr}',
      signature: '${sign.signature}',
      jsApiList: [
        'checkJsApi',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'openLocation',
        'getLocation'
      ]
  });  
</script>


<script type="text/javascript">
  wx.ready(function () {
	  // 5 图片接口
	  // 5.1 拍照、本地选图
	  var images = {
	    localId: [],
	    serverId: []
	  };

	  $(".uploadImage").click(function(){
 	      var that =$(this);
	      images.localId = [];
	      wx.chooseImage({
	        success: function (res) {
	          images.localId = res.localIds;
	          if (images.localId.length == 0) {
	              alert('请先使用 chooseImage 接口选择图片');
	              return;
	          }
	           if(images.localId.length > 3) {
	              alert('目前支持3张以内的图片上传,请重新上传');
	              images.localId = [];
	              return;
	          } 
	           
	          var i = 0, length = images.localId.length;
	          images.serverId = [];
	          
	          function upload(index) {
                  wx.uploadImage({
                        localId: images.localId[index],
                        success: function (res) {                                                              
                          var picId="";                               
                          $.get('${smvc}/business/dataoper_img_upload/${utilParam.objectid}/${utilParam.dataid}/'+that.attr('alt')+'/'+res.serverId,'', 
                          function(data,state){
                          	var strs= new Array(); //定义一数组 
                          	strs=data.split(","); //字符分割 
                          	that.parent("").parent("").append("<li><a href='${smvc}/business/dataoper_img/${utilParam.sysmoduleid}/${utilParam.objectid}/${utilParam.dataid}/${utilParam.operid}/"+strs[0]+"'><img style='height:62px;' src='${smvc}/"+strs[1]+"'></a></li>"); 
                          	//alert('${smvc}/business/dataoper_img/${utilParam.sysmoduleid}/${utilParam.objectid}/${utilParam.dataid}/${utilParam.operid}/'+strs[0]);
                          });
                          //that.parent("").parent("").append("<li><a href='${smvc}/"+picId+"'><img style='height:62px;' src='"+images.localId[index]+"'></a></li>");
                            images.serverId.push(res.serverId);

                            index++;
                            if(length>1){
                          	  alert('已上传：' + index + '/' + length); 
                            }
                            if (index < length) {
                                    upload(index);
                            }
                        },
                        fail: function (res) {
                                    alert(JSON.stringify(res));
                         }
                   });
	          }
	          if(confirm("确定上传所选图片？")){ upload(i);}
	          
	        }
	    }); 
	});
});
</script>